﻿
var path = require( 'path' );
var async = require( 'async' );
var wfContext = require( path.join( __dirname, "/Database/WorkFlowContext" ) );
var nodeContext = require( path.join( __dirname, "/Database/WorkFlowNodeContext" ) );

//依据config文件初始流程数据库
exports.init = function ( configPath ) {

    var config = require( configPath );

    if ( config != null ) {

        var identify = config.WorkFlowIdentify,
            Name = config.Name;

        async.series(
            {
                //初始化WorkFlow
                InitWorkFlow: function ( initcallback ) {

                    var existed = false;

                    async.series(
                        {
                            hasWorkFlowExisted: function ( callback ) {

                                wfContext.HasWorkFlowExisted( identify, function ( err, result ) {
                                    existed = result;
                                    callback( err, result );
                                });
                            },

                            deleteWorkFlowByIdentify: function ( callback ) {

                                if ( existed ) {

                                    wfContext.DeleteWorkFlowByIdentify( identify, function ( err, rows ) {
                                        existed = false;
                                        callback( err, rows );
                                    });
                                } else {
                                    callback( null, null );
                                }

                            },

                            insertWorkFlow: function ( callback ) {

                                if ( !existed ) {

                                    wfContext.InsertWorkFlow( {

                                        WorkFlowIdentify: config.WorkFlowIdentify,
                                        Name: config.Name,
                                        Description: config.Description

                                    }, function ( err, rows ) { callback( err, rows ); });
                                }
                            }
                        },

                        function ( err, results ) {

                            if ( err == null || err == undefined ) {
                                console.log( "Init WorkFlow of " + Name + " Success!!" );
                                initcallback( null, null );
                            }
                            else {
                                console.dir( err );
                            }
                        });
                },

                //初始化WorkFlowNode
                InitWorkFlowNode: function ( initcallback ) {

                    if ( config.Process ) {

                        var workFlowID = 0,
                            nodeList = [],
                            existed = false;

                        async.series(
                            {
                                getWorkFlowID: function ( callback ) {
                                    wfContext.GetWorkFlowByIdentify( identify, function ( err, row ) {
                                        workFlowID = row.Id;
                                        callback( err, row );
                                    });
                                },

                                analyzeProcess: function ( callback ) {
                                    for ( var key in config.Process ) {
                                        var Node = config.Process[key];
                                        if ( Node != null ) {

                                            Node.WorkFlowId = workFlowID;
                                            Node.condition = "";
                                            Node.handlerModule = "";
                                            Node.handlerFunction = "";

                                            if ( Node.handler ) {
                                                Node.handlerModule = Node.handler.moduleName;
                                                Node.handlerFunction = Node.handler.functionName;
                                            }

                                            if ( Node.type == "decision" ) {
                                                if ( Node.conditions ) {
                                                    for ( var ope in Node.conditions ) {

                                                        var newNode = JSON.parse( JSON.stringify( Node ) );

                                                        newNode.condition = ope;
                                                        newNode.transition = Node.conditions[ope];

                                                        nodeList.push( newNode );

                                                    }
                                                }
                                            } else {
                                                nodeList.push( Node );
                                            }
                                        }
                                    }
                                    //console.dir( nodeList );
                                    callback( null, nodeList );
                                },

                                hasWorkFlowNodesExisted: function ( callback ) {
                                    nodeContext.HasWorkFlowNodesExisted( workFlowID, function ( err, result ) {
                                        existed = result;
                                        callback( null, existed );
                                    });
                                },

                                clearWorkFlowNodes: function ( callback ) {
                                    if ( existed ) {

                                        nodeContext.ClearWorkFlowNodes( workFlowID, function ( err, result ) {
                                            existed = false;
                                            callback( err, result );
                                        });

                                    } else {
                                        callback( null, null );
                                    }

                                },

                                insertWorkFlowNodes: function ( callback ) {

                                    if ( !existed ) {
                                        nodeContext.InsertWorkFlowNodes( nodeList, function ( err, rows ) {
                                            callback( err, rows );
                                        });
                                    }

                                }
                            },
                            function ( err, results ) {
                                if ( err == null || err == undefined ) {
                                    console.log( "Init WorkFlowNode of " + Name + " Success!!" );
                                } else {
                                    console.dir( err );
                                }
                            });
                    }
                }
            }
            , function ( err, res ) {
                if ( err ) {
                    console.log( "Init failed! Please retry;" )
                }
            });
    }

};
